import { EnumUserType } from '../../utils/constant.js'
const app = getApp(),url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:'',//用户信息
    apply:0,
    approval: 1, // 审核状态
    collection:0,
    interest:0,
    status:1,//是不是添加了简历
    userdetail:'',///是不是显示 学历 年龄 经验
    isjianli:1,
    experience:'无',
    education:'无',
    age:'无',
    birthday:'',
    userType:'',
    enterprisedata:{},
    isLogin: false,
    nickName:'',
    elAppls: '', 
    elJzyy: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 字体样式
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#ffffff'
    })
  },

  toQiYe(){
    // wx.navigateTo({
    //   url: '/Company/newqiye/newqiye',
    // })
    wx.navigateTo({
      url: '/Company/login/login',
    })
  },
  
  toGeRen(){
    wx.navigateTo({
      url: '/HomePage/login/login',
    })
  },

  // 登录
  denglu(){
    wx.navigateTo({
      url: '/HomePage/login/login',
    })
  },
  
  setup(e){
    let userType = wx.getStorageSync("userType")
    let userInfo = null
    if(userType === EnumUserType.QiYe){
      userInfo = wx.getStorageSync("userInfo")
    } else{
      userInfo = wx.getStorageSync('user_massage')
    }
    if(!userInfo){
      wx.showToast({
        title: '请先登录',
        icon:'none',
      })
      return false
    }

    if(this.data.elAppls == 1){
      wx.showToast({
        title: '请耐心等待用户信息审核...',
        icon:'none',
      })
      return
    }
    if(this.data.elAppls == 3){
      let userMessage = wx.getStorageSync('user_massage')
      wx.showModal({
        title: '审核失败',
        content: '用户信息未通过审核，不能操作，原因：'+ this.data.elJzyy,
        confirmText: '重新注册',
        success(res){
          if(res.confirm){
            wx.navigateTo({
							url: '/HomePage/sf/sf?openid='+ userMessage.openid + '&type=open_id',
						})
            return
          }
        }
      })
      return
    }
    var url = e.currentTarget.dataset.url
    
    wx.navigateTo({ url })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({ selected: 3 })
    }
    
    let userType = wx.getStorageSync('userType')
    let userInfo = wx.getStorageSync("userInfo")
    this.setData({
      userType,
      isLogin: !!userType
    })
    if(userType == EnumUserType.QiYe){
      var  _this = this
      wx.request({
        url: url + 'enterprise/getEnterpriseInfo?enterprise_id='+ userInfo.id,
        method: "GET",
        success(res){
          if(res.data.code == 200){
             _this.setData({
              enterprisedata: res.data.data,
            })
            wx.setStorageSync("userInfo", res.data.data)
          }
        }
      })

    }else{
      var that = this
      var user_massage = wx.getStorageSync('user_massage')
      if(user_massage){
        that.setData({isjianli:2})
        wx.request({
          url: url + 'cvBaseInfo/getCVBaseInfoByUserUid?user_uid='+user_massage.userUid,
          method: "GET",
          success:function(res){
            var ret = res.data
            if(ret.code == 200){
              if(ret.data == undefined){
                that.setData({avatarUrl:userInfo.avatarUrl,nickName:userInfo.nickName,})
              }else{
                that.setData({
                  id:ret.data.id,//简历id
                  avatarUrl:ret.data.header?ret.data.header:userInfo.avatarUrl,//touxiang
                  nickName:ret.data.name || userInfo.nickName,
                  age:Number(ret.data.age),
                  education:ret.data.education,
                  birthday:ret.data.birthday,
                  elAppls: ret.data.elAppls || 1,
                  elJzyy: ret.data.elJzyy
                })

              }
            }else{
              wx.showToast({
                title: '数据有误',
                icon:'none'
              })
            }
          }
        })
        // 三个数值
        wx.request({
          url: url + 'wxMine/threeTotal',
          method: "GET",
          data:{
            user_uid:user_massage.userUid,
          },
          success:function(res){
            console.log(" 三个数值",res);
            var ret = res.data
            if(ret.code == 200){
              that.setData({
                apply:ret.data.apply,
                collection:ret.data.collection,
                interest:ret.data.interest,
              })
            }else{
              wx.showToast({
                title: '数据有误',
                icon:'none'
              })
            }
          }
        })
      }else{
        that.setData({
          isjianli:1,
          apply:0,
          collection:0,
          interest:0,
        })
        // wx.showModal({
        //   title: '提示',
        //   content: '你还未登录，是否前往登录？',
        //   success (res) {
        //     if (res.confirm) {
        //       wx.navigateTo({
        //         url: '/HomePage/login/login',
        //       })
        //     } else if (res.cancel) {
        //       wx.switchTab({
        //         url: '/pages/index/index',
        //       })
        //     }
        //   }
        // })
      }
    }
  },
  showJZYY(){
    wx.showModal({
      title: '提示',
      content: this.data.enterprisedata.jzyy,
      showCancel: false
    })
  },
  showELJZYY(){
    let userMessage = wx.getStorageSync('user_massage')
    wx.showModal({
      title: '审核失败',
      content: '用户信息未通过审核，不能操作，原因：'+ this.data.elJzyy,
      confirmText: '重新注册',
      success(res){
        if(res.confirm){
          wx.navigateTo({
            url: '/HomePage/sf/sf?openid='+ userMessage.openId + '&type=rereg',
          })
          return
        }
      }
    })
  },
  
  //跳转职位管理
  jumpPosition(e){
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url
    })
  },
  //跳转招聘会管理
  jumpJobfair(e){
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },
  //
  jumpNewqiye(e){
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },
  //跳转简历管理
  jumpjianligl(e){
    wx.navigateTo({
      url: '/Company/jianligl/jianligl?jltype='+ e.currentTarget.dataset.type,
    })
  },
  infor(){
    var user_massage = wx.getStorageSync('user_massage')
    // console.log(user_massage)
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    var that = this
    that.xcx_user_login()
  }, 
  xcx_user_login(){
    var openid = wx.getStorageSync('openid')
    if(openid){
      var that = this
      wx.request({
        url:url+'wx_commen/user_login',
        method: "GET",
        data:{
          ask_type:'open_id',
          open_id:openid,
          phone:'',
        },
        success:function(resh){
          console.log("openid",resh);
            wx.setStorageSync('user_massage', resh.data.user_massage)

        }
      })
    }else{
      console.log(1)
    }
  },
  //跳转账号密码
  jumpzhmima(e){
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },
  onRoleChange(){
    wx.clearStorageSync()
    wx.reLaunch({
      url: '/pages/my/my',
    })
  }
})